<?php 
include_once 'function.php'; 
$conn = conn();
$pagenum = !isset($_GET['p']) || $_GET['p'] < 1 ? 1 : $_GET['p'];
// 从第二页开始收费
if ($pagenum >= 2)
{
    if (!check_permission())
    {
    	header('Location:/price.php');
    	exit;
    }
}
?><!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<link rel="stylesheet" type="text/css" href="/theme.css" />
		<script language="javascript">
		// change the page width according to orientation
		var updateOrientation = function(){
			if ('undefined' != typeof window.orientation)
			{
				var className = 'wrapper_';
				switch(window.orientation){
					case 0:
						className += 'portrait';
						break;
					case -90:
						className += 'landscape';
						break;
					case 90:
						className += 'landscape';
						break;
					case 180:
						className += 'portrait';
						break;
				}
				document.getElementById('wrapper').setAttribute('class', className);
			}
		}
		window.onload = updateOrientation;
		window.onorientationchange = updateOrientation;
		</script>
	</head>
	<body>
		<div id="wrapper" class="wrapper_landscape">
			<div class="header">
				<div class="logo"><a href="/"><?php echo SITE;?></a></div>
				<div class="welcome">
				<?php if (isset($_SESSION['id']) && !empty($_SESSION['id'])) 
				{
					$id = $_SESSION['id'];
					$sql = "SELECT name, email FROM accounts WHERE id = $id";
					$result = mysql_query($sql, $conn);
					$row = mysql_fetch_assoc($result);
					$name = $row['name'];
					$email = $row['email'];
					$name = empty($name) ? substr($email, 0, strpos($email, '@')) : $name;
					echo '欢迎您, '.$name.' | <a href="/charge.php?f=1">使用充值密码</a> | <a href="/logout.php">退出</a>';
				}
				else 
				{
					echo '<a href="/login.php">登录</a> | <a href="/register.php">免费注册</a>';
				}
				?>
				</div>
				<div class="clear"></div>
			</div>
			<ul class="container">
<?php
$limit = 9;
$start = ($pagenum - 1) * $limit;

$sql = "SELECT id, name, img FROM films ORDER BY free DESC LIMIT $start, $limit";
$result = mysql_query($sql, $conn);
$films = array();
while ($row = mysql_fetch_assoc($result))
{
    $films[] = $row;
}
for ($i = 0; $i < count($films); $i ++)
{
    $id = $films[$i]['id'];
    $film_name = $films[$i]['name'];
    $img = $films[$i]['img'];
    echo '<li', (2 == $i % 3 ? ' class="last"' : ''), '>',
        '<a href="/play/', $id, '/"><img src="/imgs/', $img, '" /></a><br />', $film_name ,
        '</li>';
}
?>
			</ul>
			<div class="clear"></div>
			<div class="paginate">
<?php 
$sql = "SELECT COUNT(*) c FROM films";
$result = mysql_query($sql, $conn);
$row = mysql_fetch_assoc($result);
$total = $row['c'];
$totalpages = ceil($total / $limit);
echo paginate($totalpages, $pagenum, 'index'); 
?>
			</div>

			<div class="footer">
				<a href="/payment.php">支付方式</a>
				<a href="/price.php">服务价格</a>
				<a href="/contact.php">联系我们</a><br /><br />
				&copy; <?php echo SITE.' '.date('Y');?>
			</div>
		</div>
	</body>
</html>